package cn.itcast.service.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

public class JedisUtil {
    private static JedisPool jedisPool ;

    //加载jedis.properties的文件只需要加载一次就够，所以我们把加载配置文件的方法放入静态代码块中
    static{
        //1. 加载jedis.properties
        /*
             加载properties文件有两种方式：
                1. Properties类加载(需要你自己使用类路径得到properties的输入流)
                2. ResourceBundle 加载(底层本身就已经封装好类路径获取properties文件了)
         */
        ResourceBundle resourceBundle = ResourceBundle.getBundle("jedis/jedis");//注意：后缀名不能写
        Integer maxWaitMillis = Integer.valueOf(resourceBundle.getString("maxWaitMillis"));
        Integer maxTotal = Integer.valueOf(resourceBundle.getString("maxTotal"));
        Integer port = Integer.valueOf(resourceBundle.getString("port"));
        String host = resourceBundle.getString("host");

        //2.创建jedis连接池配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxTotal);

        //3.创建jedis的连接池
        jedisPool = new JedisPool(jedisPoolConfig,host,port);
    }


    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}
